<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit sends.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Sends_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["send"] = new Send($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["send"]->id);

    // To one relationships
        
    $campaigns = new Campaign();
    $data["campaigns"] = $campaigns->all_to_asoc_array("id","subject");
    $data["campaigns_selected"] = $data["send"]->campaign->id;
        
    $recipients = new Recipient();
    $data["recipients"] = $recipients->all_to_asoc_array("id","email");
    $data["recipients_selected"] = $data["send"]->recipient->id;
    
    // To many relationships
    
    $this->smarty_parser->view('send_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["send"] = new Send($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["send"]->read = $this->input->post("read");
      if ($data["send"]->read == "") $data["send"]->read = NULL;
      
      // files for upload
      /*
      if ($data["send"]->read->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(read)) $data["send"]->read = $fileupload.clean_file_name(read);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["send"]->tries = $this->input->post("tries");
      if ($data["send"]->tries == "") $data["send"]->tries = NULL;
      
      // files for upload
      /*
      if ($data["send"]->tries->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(tries)) $data["send"]->tries = $fileupload.clean_file_name(tries);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["send"]->sent = $this->input->post("sent");
      if ($data["send"]->sent == "") $data["send"]->sent = NULL;
      
      // files for upload
      /*
      if ($data["send"]->sent->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(sent)) $data["send"]->sent = $fileupload.clean_file_name(sent);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["send"]->bounced = $this->input->post("bounced");
      if ($data["send"]->bounced == "") $data["send"]->bounced = NULL;
      
      // files for upload
      /*
      if ($data["send"]->bounced->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(bounced)) $data["send"]->bounced = $fileupload.clean_file_name(bounced);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["send"]->awsmessageid = $this->input->post("awsmessageid");
      if ($data["send"]->awsmessageid == "") $data["send"]->awsmessageid = NULL;
      
      // files for upload
      /*
      if ($data["send"]->awsmessageid->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(awsmessageid)) $data["send"]->awsmessageid = $fileupload.clean_file_name(awsmessageid);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["send"]->save())
      $data["error"] .= $data["send"]->error->string;

    //saving to one relationships
    
    $campaign = new Campaign($this->input->post("campaign"));
    if (! $data["send"]->save(array($campaign)))
      $data["error"] .= $data["send"]->error->string;
    
    $recipient = new Recipient($this->input->post("recipient"));
    if (! $data["send"]->save(array($recipient)))
      $data["error"] .= $data["send"]->error->string;
    
    //to many relationships
    
    redirect(base_url()."sends/edit/".$data["send"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["send"] = new Send($id);

    //delete
    if(! $data["send"]->delete())
      $data["error"] .= $data["send"]->error->string;

    redirect(base_url()."sends/edit/");
  } // end of method delete($id)

    
} // class Sends_protoweb

/* End of file sends_protoweb.php */
/* Location: ./system/application/controllers/sends_protoweb.php */
